<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mInstall
 *
 * @author prezes
 */
class mInstall extends mModel{
   
    public function writeConf(){
        write_ini_file( array( 'hostname' =>    PostFiltered('hostname'),
                                'username' =>   PostFiltered('username'),
                                'port' =>       PostFiltered('port')== null ? 3306 : PostFiltered('password'),
                                'password' =>   PostFiltered('password') ,
                                'database' => PostFiltered('database')  ),
                './config/dbconf.ini');
        
    }
    
    public function connectionFormValidate(){
        
            $this->dbHostname= PostFiltered('hostname');
            $this->dbPort = PostFiltered('port');
            $this->dbUsername = PostFiltered('username');
            $this->dbPassword = PostFiltered('password');
            $this->dbDatabase = PostFiltered('database');
            
            
        
            if(PostFiltered('hostname')=='') $this->error[] = "Hostname cannot be NULL";
            if(PostFiltered('username')=='') $this->error[] = "Username cannot be NULL";
            if(PostFiltered('database')=='') $this->error[] = "Database cannot be NULL";

            $this->writeConf();
            
            if(empty($this->error)){
                
                $this->connectionInfo();
                                
            }else{
                $this->model->connectionForm();
            } 
    }
    
      
    
    public function connectionForm(){
        $this->string = "<form action = 'install.php' method = 'post'>"
                . "<input type = 'hidden' name = 'method' value= 'connect'>"
                . "Hostname: <input type = 'text' name = 'hostname' value = '" . PostFiltered('hostname') . "' ><br>"
                . "Port: <input type = 'text' name = 'port' value = '" . PostFiltered('port') . "' ><br>"
                . "Username: <input type = 'text' name = 'username' value = '" . PostFiltered('username') . "' ><br>"
                . "Password: <input type = 'password' name = 'password' ><br>"
                . "Database: <input type = 'text' name = 'database' value = '" . PostFiltered('database') . "' ><br>"
                . "<input type = 'submit'>"
                . "</form>";
    }
    
    public function connectionInfo(){
        $this->string = "";
        
        if( !is_null(DbConnection::getInstance()) ){
           
            $this->message[] =  "Connection Sucesfull";
            
            $this->string="<form action = 'install.php' method = 'post'>"
                    . "<input type = 'hidden' name = 'method' value = 'importdb' >"
                    . "<input type = 'submit' value = 'Continue' >"
                    . "</form>";
            
        }else{
            $this->error[] =  "Connection Failed!";
            
            $this->string="<form action = 'install.php' method = 'post'>"
                    . "<input type = 'submit' value = 'Back' >"
                    . "</form>";
            
        }
                
    }
    
   
    
    public function configForm(){
        $this->string = "<form action = 'install.php' method = 'post'  >"
                . "<input type = 'hidden' name = 'method' value = 'upload'>"
                . "Title: <input type = 'text' name= 'title' ><br>"
                . "Favicon: <input type = 'file' name = 'favicon' allowed = '.ico' ><br>"
                . "Logo: <input type = 'file' name = 'logo' allowed = '.jpg' ><br>"
                . "Root username: <input type = 'text' name = 'username' value = 'root'><br>"
                . "Root password: <input type = 'password' name = 'password' ><br>"
                . "<input type = 'submit' value = 'continue' >"
                . "</form>";
    }
    
    public function instaled(){
        $this->message[] = 'Instalation complete';
        $this->error[] = '! Please delete file install.php and change permisions for config/dbconf.ini !';
        $this->string = "<form action = 'index.php' moethod = 'get'>"
                . "<input type = 'submit'>"
                . "</form>";
    }
}
